package commons;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthenticationFilter1 implements Filter {
  private FilterConfig config;
  
    @Override
  public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException {
    if (((HttpServletRequest) req).getSession().getAttribute(AuthenticationBean.AUTH_KEY) == null) {
      ((HttpServletResponse) resp).sendRedirect("../login.xhtml");
    } else if(!((HttpServletRequest) req).getSession().getAttribute(AuthenticationBean.AUTH_TYPE).equals("magazyn")){
      ((HttpServletResponse) resp).sendRedirect("../login.xhtml");
    }else{
      chain.doFilter(req, resp);
    }
  }

    @Override
  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }

    @Override
  public void destroy() {
    config = null;
  }
}

